import Vue from 'vue';
import VueRouter from 'vue-router';
import {
	proTit
} from '@/utils/config'
import store from '@/store'
import {
	getToken
} from '@/utils/auth'
import {i18n} from '@js/lang'
var lang = store.getters.lang.loc

const app = i18n.messages[lang||'zh'].app
Vue.use(VueRouter);
//console.log("i18n router",app.Index)
//登录注册相关
let LoginPort = () => import('@/view/login/login')
let Login = () => import('@/view/login/index')
let Forget = () => import('@/view/login/forgetPassword')
//let ResetPassword = () => import('@/view/login/resetPassword')
//训练课程

let Index = () =>import('@/view/index');
let TrainPort = () => import('@/view/index/train')
let TrainDetails = () => import('@/view/index/train/details')
let TrainDetailsEdit = () => import('@/view/index/train/edit');

let BarcodeQr = () => import('@/components/barcodeQr') 
let Review = () => import('@/view/index/review')
let LessonCoach = () => import('@/view/index/lessonCoach')
let Participant = () => import('@/view/index/lessonCoach/participant')
// console.log("rolesroles",'COACH'.indexOf(/'ADMIN''COACH'/))
const ClickLeftRoute = (e, vm,opt={})=>{
							if(opt!=={} && opt&&vm)	{
								vm.$router.push(opt)
							}
						}
const routes = [
		{
		path: '/LoginPassPage',
		name: 'LoginPassPage',
		redirect: '/login',
		component: LoginPort,
		meta: {
			noLogin: true
		},
		children: [
			// {
			// 	path: '/resetPassword',
			// 	name: 'ResetPassword',
			// 	component: ResetPassword,
			// 	meta: {
			// 		noLogin: true,
			// 		title: '修改密码'
			// 	}
			// },
			{
				path: '/login',
				name: 'Login',
				component: Login,
				meta: {
					noLogin: true,
					title: app.Login._title,//'登录',
					hideHeader: true,
					hideBackIcon: true
				}
			},
			{
				path: '/forget',
				name: 'ForgetPassword',
				component: Forget,
				meta: {
					title:app.ForgetPassword._title,//'忘记密码',
					noLogin: true,
					hideGoBack: true,
					noLoginRequired: true,
					onClickLeft:(e,vm)=> ClickLeftRoute(e,vm,{name:'Login'})
				}
			}
		]
		},
		{
			name: 'demo',
			path:'/videoDemo',
			meta: {
			},
			component:() => import('@/components/demo'),
			},
	{
		path: '/',
		name: '_Index',
		redirect: 'index',
		component: () => import('@/view/index/training'),
		meta: {
			title: app.Index._title,//'培訓考勤',
			//onClickLeft:()=>store.dispatch('user/logout')
		},
		children: [{
				name: 'Index',
				path: "index",
				component: Index,
				meta: {
					title: app.Index._title,//'Training Attendance',
					hideBackIcon: true,
					rightText: app.Index.rightText,//'我的二維碼',
					onClickRight: (e, vm) => {
						var paramss = store.state.train.params.qr
						let params = typeof(paramss)==='string'? paramss :''
						vm.$router.push({name:'BarcodeQr',query:{params}})
					},
					leftText:app.Index.leftText, //'退出登录',
					onClickLeft: () => store.dispatch('user/logout')
				}
			},
			{
				name: 'Train',
				path:'/train',
				redirect: "/train/trainDetails",
				meta: {
				},
				component: TrainPort,
				children: [
					{name: 'TrainDetails',
					path: "details",
					component: TrainDetails,
					meta: {
						title: '',
						leftText: app.TrainDetails.leftText,
						rightText: ['SUPER_ADMIN','HEAD_COACH','COACH','ADMIN'].indexOf(store.getters.roles)>-1?app.TrainDetails.rightText:'',//"编辑",
						onClickRight: (e, vm) => {
							vm.$router.push({name:'TrainDetailsEdit',query:{lessonId:vm.$route.query.lessonId}})
						},
						onClickLeft:(e,vm)=> ClickLeftRoute(e,vm,{name:'Index'})
						}
					},
					{
						path:'edit',
						name:'TrainDetailsEdit',
						component: TrainDetailsEdit,
						meta: {
							title: '',
							leftText: '',
							rightText: app.TrainDetailsEdit.rightText,
							onClickLeft:(e,vm)=> ClickLeftRoute(e,vm,{name:'TrainDetails',query:vm.$route.query})
						},
					},
					{
						name: 'LessonCoach',
						path: "/lessonCoach",
						component: LessonCoach,
						meta: {
							title: '',
							leftText: '',
							rightText: app.LessonCoach.rightText,//'储存'
							onClickLeft:(e,vm)=> ClickLeftRoute(e,vm,{name:'TrainDetails',query:{lessonId:vm.$route.query.lessonId}})
						}
					},
					{
						name: 'Participant',
						path: "/participant",
						component: Participant,
						meta: {
							title:'',
							reload:true,
							leftText: '',
							rightText: app.LessonCoach.rightText,//'储存'
							onClickLeft:(e,vm)=> ClickLeftRoute(e,vm,{name:'TrainDetailsEdit',query:{lessonId:vm.$route.query.lessonId}})
						}
					}


				],

			},

			{
				name: 'BarcodeQr',
				path: "barcodeQr",
				component: BarcodeQr,
				meta: {
					title: '',//'Training Attendance',
					//leftText: ''
					onClickLeft:(e,vm)=>{
						let query = vm.$route.query.lessonId
						ClickLeftRoute(e,vm,{name:query?'TrainDetails':'Index',query:query?{lessonId:query}:{}})
					} 
				}
			},
			{
				name: 'Review',
				path: '/review',
				component: Review,
				meta: {
					title:  app.Review._title,//'确认/检阅',
					onClickLeft:(e,vm)=> ClickLeftRoute(e,vm,{name:'TrainDetails',query:{lessonId:vm.$route.query.lessonId}})
				}
			}
			
		]
	}
];

// add route path
routes.forEach(route => {
		
	route.path = route.path || '/' + (route.name || '');
	console.log('实打实',app[route.name],app,route)
	if(app[route.name]){
		console.log('ddddd',app,route)
		route.meta.title = app[route.name]._title
		document.title = app[route.name]._title
		route.meta.leftText =app[route.name].leftText
	}
	
});

const createRouter = () => new VueRouter({
	//mode: 'history', // require service support
	base: proTit,
	routes
	//linkExactActiveClass: 'current'

})
const router = createRouter()
router.beforeEach((to, from, next) => {
	//var scrollTop = document.getElementById('app').scrollTop
	var Token = getToken()
	// scrollTop = 0
	//console.log('page-box', scrollTop)
	const title = to.meta && to.meta.title;
	if (title) {
		document.title = title;
	}
	//如果跳回登录页面，清空所有数据

	//判断页面是否需要登录才可以访问， 没登录就跳到登录页
	if (!to.meta.noLogin) {
		if (!Token) {
			return next({
				name: 'Login'
			})
		}
	} else {
		if (Token) {
			return next({
				name: 'Index',
				meta: {
					hideBackIcon: true
				}
			});
		}
	}
	next()
});
export const resetRouter = () => {
	const newRouter = createRouter()
	router.matcher = newRouter.matcher // reset router
}
export default router
